package IO.统计文件夹大小;

import java.io.File;
import java.util.Arrays;
import java.util.LinkedList;

public class WhileSize {

    /**
     * @param args
     */
    public static void main(String[] args) {
        File file = new File("D:\\tmp");
        long startTime = System.currentTimeMillis();
        System.out.println(file.getName() + " total size is "
                + getFileSize(file));
        System.out.println("getFileSize cost time is "
                + (System.currentTimeMillis() - startTime));
    }

    private static long getFileSize(File file) {
        long filesSize = 0l;
        LinkedList<File> list = new LinkedList<File>();
        if (file.isFile()) {
            return file.length() / 1024;
        } else {
            File[] files = file.listFiles();
            list.addAll(Arrays.asList(files));
            while (!list.isEmpty()) {
                File tmpFile = list.removeFirst();
                if (tmpFile.isFile()) {
                    filesSize = filesSize + tmpFile.length();
                } else {
                    list.addAll(Arrays.asList(tmpFile.listFiles()));
                }
            }
            return filesSize / 1024 / 1024;
        }

    }

}